<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>商城后台</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" th:href="@{/lib/layui-v2.5.5/css/layui.css}" media="all">
    <link rel="stylesheet" th:href="@{/css/public.css}" media="all">
    <link rel="stylesheet" th:href="@{/lib/layui_ext/dtree/dtree.css}" >
    <link rel="stylesheet" th:href="@{/lib/layui_ext/dtree/font/dtreefont.css}" >
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>

<span th:replace="common_page::common"></span>

<div class="layui-form layuimini-form">

    <div class="layui-form-item">
        <label class="layui-form-label required">用户范围</label>
        <div class="layui-input-block">
            <select id="userScope"  name="userScope" lay-search="" lay-filter="userScope">
                <!--<option value="-1">全部用户</option>-->
                <option value="1">指定用户</option>
            </select>
        </div>
    </div>
    <div class="layui-form-item" id="receiveUserListDiv" style="display: block">
        <label class="layui-form-label required">接收用户</label>
        <div class="layui-input-block">
            <!--最大限制选择500个用户 -->
            <textarea id="userMainIdSelect" name="userMainIdListString" class="layui-textarea" maxlength="9500" placeholder="请输入消息内容"></textarea>
            <tip>填写用户ID。</tip>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label required">消息标题</label>
        <div class="layui-input-block">
            <input type="title" name="title" lay-verify="required" maxlength="255" lay-reqtext="不能为空" placeholder="请输入消息标题" value="" class="layui-input">
            <tip>填写标题。</tip>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label required">发送时间</label>
        <div class="layui-input-block">
            <input type="title" id="sendDate" name="sendDate" lay-verify="required"  lay-reqtext="不能为空" placeholder="请输入发送时间" value="" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">消息内容</label>
        <div class="layui-input-block">
            <textarea name="content" class="layui-textarea" maxlength="500" placeholder="请输入消息内容"></textarea>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label required">消息类型</label>
        <div class="layui-input-block">
            <select id="messageTypeCode" name="messageTypeCode"  lay-search="">
                <option th:each="mt : ${messageTypes}" th:value="${mt.code}" th:text="${mt.name}">
            </select>
        </div>
    </div>



    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
        </div>
    </div>
</div>
<script th:src="@{/lib/layui-v2.5.5/layui.js}" charset="utf-8"></script>
<script>


    layui.config({
        base: basePath+'/lib/layui_ext/dtree/' //配置 layui 第三方扩展组件存放的基础目录
    }).use(['form','laydate'], function () {
        var form = layui.form,
            layer = layui.layer,
            $ = layui.$;
        var laydate = layui.laydate;



        //初始化时间控件
        laydate.render({
            elem: '#sendDate' //指定元素
            ,type: 'datetime'
        });


        $("#userMainIdSelect").click(function(){
            var index = layer.open({
                title: '选择用户列表',
                type: 2,
                shade: 0.2,
                maxmin:true,
                btn: ["<i class='layui-icon layui-icon-ok'></i>确定", "关闭" ],
                yes: function (index) {
                    var userMainIdListString = window["layui-layer-iframe" + index].getSelectUserMainIdArray();
                    setUserMainId(userMainIdListString);
                    layer.close(index);
                },
                area: ['80%', '80%'],
                content: "[[@{/message/messageUser/selectUserListPage}]]",
                zIndex: layer.zIndex,
                success: function(layero){
                    layer.setTop(layero);
                }
            });
            $(window).on("resize", function () {
                layer.full(index);
            });
        });




        function existsByUserMainId(selectUserMainId,userMainIdArray)
        {
            if(userMainIdArray!=null&&userMainIdArray.length>0)
            {
                for(var i=0;i<userMainIdArray.length;i++)
                {
                    if(userMainIdArray[i]==selectUserMainId)
                    {
                        return true;
                    }
                }
            }
            return false;
        }

        function setUserMainId(selelectUserMainIdList)
        {
            var userMainIdValues = $("#userMainIdSelect").val();
            if(userMainIdValues!=null&&userMainIdValues!="")
            {
                var userMainIdArray = userMainIdValues.split(",");
                for(var i=0;i<selelectUserMainIdList.length;i++)
                {
                    var selectUserMainId = selelectUserMainIdList[i];
                    //去重
                    if(!existsByUserMainId(selectUserMainId,userMainIdArray))
                    {
                        userMainIdArray.push(selectUserMainId);
                    }
                }
                $("#userMainIdSelect").val(userMainIdArray.join(","));
            }else{ //首次保存
                $("#userMainIdSelect").val(selelectUserMainIdList.join(","));
            }

        }



        form.on('select(userScope)', function(data){
            if(data.value=="-1")
            {
                $("#receiveUserListDiv").css('display','none');
            }else if(data.value=="1"){
                $("#receiveUserListDiv").css('display','block');
            }
        });

        //监听提交
        form.on('submit(saveBtn)', function (data) {
            $.ajax({
                url:"[[@{/message/messageUser/save}]]",
                contentType: "application/json; charset=utf-8",
                type:'POST',
                data:JSON.stringify(data.field),
                success:function(data){
                    if(data.code<=0) {
                        layer.msg(data.msg);
                        return false;
                    }
                    var index = layer.alert(data.msg, {
                        title: '提示信息'
                    }, function () {
                        // 关闭弹出层
                        layer.close(index);
                        var iframeIndex = parent.layer.getFrameIndex(window.name);
                        parent.location.reload();
                        parent.layer.close(iframeIndex);

                    });
                }
            });
            return false;
        });


    });
</script>
</body>
</html>